package es.deusto.tralala.rmi.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.DefaultTableModel;

import es.deusto.tralala.data.dto.CancionDTO;
import es.deusto.tralala.rmi.client.controller.TralalaController;

public class MainWindow extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private TralalaController controller;
	private CancionDTO cancionSelected = null;

	private JButton bBuscar;
	private JButton bReproducir;
	private JButton bPausar;
	private JButton bStop;
	private JTextField tCancion;
	private DefaultTableModel modelo;
	private JScrollPane scroll;
	private JTable tabla;
	// private Controller controller;
	// private List<CancionDTO> listaCanciones;
	// private CancionDTO cancion;
	// private BasicPlayer player = new BasicPlayer();
	private int indice;

	public MainWindow() throws RemoteException {
		init();
	}
	
	public MainWindow(TralalaController controller) throws RemoteException {
		init();
		
		this.controller = controller;
		
		List<CancionDTO> canciones = controller.getCanciones();
		
		if (canciones != null) {
			for (CancionDTO cancion : canciones) {
				modelo.addRow(new String[] {cancion.getTitulo(), cancion.getArtista(), cancion.getGenero()});
			}
		}
	}
	
	public void init() {
		// this.controller = controller;
		bBuscar = new JButton("Buscar");
		bReproducir = new JButton("Play");
		bPausar = new JButton("Pause");
		bStop = new JButton("Stop");
		bPausar.setEnabled(false);
		bStop.setEnabled(false);

		bReproducir.addActionListener(this);
		bPausar.addActionListener(this);
		bBuscar.addActionListener(this);
		bStop.addActionListener(this);
		tCancion = new JTextField(18);
		tCancion.setText("");
		tCancion.setSize(100, 100);

		modelo = new DefaultTableModel(new Object[][] {}, new String[] {
				"Nombre", "Autor", "Genero" }) {
			private static final long serialVersionUID = 1L;
			boolean[] canEdit = new boolean[] { false, false, false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		};

		tabla = new JTable(modelo);
		scroll = new JScrollPane();
		scroll.setViewportView(tabla);
		scroll.setPreferredSize(new Dimension());
		scroll.setBackground(Color.WHITE);

		JPanel b1 = new JPanel();
		b1.setLayout(new FlowLayout());
		b1.setBackground(Color.WHITE);
		b1.add(new JLabel("Cancion: "));
		b1.add(tCancion);
		b1.add(bBuscar);
		b1.add(bReproducir);
		b1.add(bPausar);
		b1.add(bStop);
		
		tabla.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					JTable target = (JTable) e.getSource();
					int row = target.getSelectedRow();
					int column = target.getSelectedColumn();
					cancionSelected = controller.getCancion((String) modelo.getValueAt(row, 0));
					System.out.println(cancionSelected);
				}
			}
		});

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(b1, BorderLayout.NORTH);
		getContentPane().add(scroll, BorderLayout.CENTER);
		getContentPane().setBackground(Color.WHITE);

		this.setSize(800, 400);
		this.setTitle("TRALALA");
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBackground(Color.WHITE);
	}

	public static void main(String[] args) throws RemoteException {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e1) {

		}
		MainWindow mw = new MainWindow();
		mw.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object fuente = e.getSource();
		if (fuente == bBuscar) {
			if (tCancion.getText() != null && !tCancion.getText().isEmpty()) {
				modelo.setRowCount(0);
				
				List<CancionDTO> canciones = controller.searchCancion(tCancion.getText());
				
				if (canciones != null) {
					for (CancionDTO cancion : canciones) {
						modelo.addRow(new String[] {cancion.getTitulo(), cancion.getArtista(), cancion.getGenero()});
					}
				}
			}
		} else if (fuente == bStop) {

		} else if (fuente == bPausar) {

		} else if (fuente == bReproducir) {
			if (cancionSelected != null) {
				controller.addReproduccion(cancionSelected);
			}
		}

	}
}
